package com.hjm.common.utils.request;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 
 * @author: wangzw
 * @version: 1.0
 * @date: 2018/6/29 17:57
 */
public class XmlUtil {

    /**
     * 解析xml,返回第一级元素键值对。如果第一级元素有子节点，则此节点的值是子节点的xml数据。
     * @param strXml
     * @return
     * @throws
     * @throws IOException
     */
    public static Map<String,Object> doXMLParse(String strXml) {
        InputStream in = null;
        try{
            strXml = strXml.replaceFirst("encoding=\".*\"", "encoding=\"UTF-8\"");
            if(null == strXml || "".equals(strXml)) {
                return null;
            }
            Map<String,Object> resultMap = new HashMap<>();
            in = new ByteArrayInputStream(strXml.getBytes("UTF-8"));
            SAXBuilder builder = new SAXBuilder();
            Document doc = builder.build(in);
            Element root = doc.getRootElement();
            List list = root.getChildren();
            Iterator it = list.iterator();
            while(it.hasNext()) {
                Element e = (Element) it.next();
                String k = e.getName();
                String v = "";
                List children = e.getChildren();
                if(children.isEmpty()) {
                    v = e.getTextNormalize();
                } else {
                    v = getChildrenText(children);
                }
                resultMap.put(k, v);
            }
            return resultMap;
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
     
    /**
     * 获取子结点的xml
     * @param children
     * @return String
     */
    private static String getChildrenText(List children) {
        StringBuffer stringBuffer = new StringBuffer();
        if(!children.isEmpty()) {
            Iterator it = children.iterator();
            while(it.hasNext()) {
                Element e = (Element) it.next();
                String name = e.getName();
                String value = e.getTextNormalize();
                List list = e.getChildren();
                stringBuffer.append("<" + name + ">");
                if(!list.isEmpty()) {
                    stringBuffer.append(XmlUtil.getChildrenText(list));
                }
                stringBuffer.append(value);
                stringBuffer.append("</" + name + ">");
            }
        }
        return stringBuffer.toString();
    }

    /**
     * 支付成功，返回微信那服务器
     */
    public static String setXML(String return_code, String return_msg) {
        return "<xml><return_code><![CDATA[" + return_code + "]]></return_code><return_msg><![CDATA[" + return_msg + "]]></return_msg></xml>";
    }

}